class IncidentsController < ApplicationController
before_filter :login_required, :only => [:new, :edit, :create, :update, :destroy]
  # GET /incidents
  # GET /incidents.xml
  def index
    @incidents = Incident.all

    respond_to do |format|
		format.html {render :layout => 'layout'} # index.html.erb
		format.xml  { render :xml => @incidents }
    end
  end

  # GET /incidents/1
  # GET /incidents/1.xml
  def show
    @incident = Incident.find(params[:id])

    respond_to do |format|
      format.html {render :layout => 'layout'} # show.html.erb
      format.xml  { render :xml => @incident }
    end
  end

  # GET /incidents/new
  # GET /incidents/new.xml
  def new
    @incident = Incident.new

    respond_to do |format|
      format.html {render :layout => 'layout'} # new.html.erb
      format.xml  { render :xml => @incident }
    end
  end

  # GET /incidents/1/edit
  def edit
    @incident = Incident.find(params[:id])
	
	respond_to do |format|
		format.html {render :layout => 'layout'} # index.html.erb
		format.xml  { render :xml => @incidents }
    end
  end

  # POST /incidents
  # POST /incidents.xml
  def create
    @incident = Incident.new(params[:incident])
	if(@incident.validate_numeric_machine && @incident.validate_numeric_type_incident)
		respond_to do |format|
		  if @incident.save
			flash[:notice] = 'L\'incident a &ea;t&eacute; cr&eacute;.'
			format.html { redirect_to(@incident) }
			format.xml  { render :xml => @incident, :status => :created, :location => @incident }
		  else
			format.html { render :action => "new", :layout => 'layout'}
			format.xml  { render :xml => @incident.errors, :status => :unprocessable_entity }
		  end
		end
	else
		if(!@incident.validate_numeric_machine)
			respond_to do |format|
				flash[:notice] = 'La valeur saisie pour l\'id de la machine doit &ecirc;tre un num&eacute;rique.'
				format.html { render :action => "new", :layout => 'layout' }
				format.xml  { render :xml => @incident.errors, :status => :unprocessable_entity }
				
			end
		else
			respond_to do |format|
				flash[:notice] = 'La valeur saisie pour l\'id du type d\'incident doit &ecirc;tre un num&eacute;rique.'
				format.html { render :action => "new", :layout => 'layout' }
				format.xml  { render :xml => @incident.errors, :status => :unprocessable_entity }
				
			end
		end
	end
  end

  # PUT /incidents/1
  # PUT /incidents/1.xml
  def update
    @incident = Incident.find(params[:id])

    respond_to do |format|
      if @incident.update_attributes(params[:incident])
        flash[:notice] = "L'incident a été mis à jour."
        format.html { redirect_to(@incident) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit", :layout => 'layout'}
        format.xml  { render :xml => @incident.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /incidents/1
  # DELETE /incidents/1.xml
  def destroy
    @incident = Incident.find(params[:id])
    @incident.destroy

    respond_to do |format|
      format.html { redirect_to(incidents_url) }
      format.xml  { head :ok }
    end
  end
end
